package com.titar.robot.ui.forgotpsd;


import com.titar.robot.AppExecutors;
import com.titar.robot.db.CacheTableDao;
import com.titar.robot.db.RobotDatabase;
import com.titar.robot.entity.VerifyCodeResponse;
import com.titar.robot.http.RetrofitApi;
import com.titar.robot.http.service.CustomerService;
import com.titar.robot.repository.Resource;
import com.titar.robot.utils.ToastUtils;

import androidx.lifecycle.Observer;

public class ForgotPsdPresenter implements ForgotPsdContact.Presenter {
    private ForgotPsdContact.View v;
    private ForgotPsdRepository forgotPsdRepository;

    public ForgotPsdPresenter() {
        AppExecutors appExecutors = AppExecutors.getInstence();
        CacheTableDao cacheTableDao = RobotDatabase.getDatabase().cacheTableDao();
        CustomerService customerService = RetrofitApi.getRequestInterface();
        forgotPsdRepository = new ForgotPsdRepository(appExecutors, cacheTableDao, customerService);
    }


    @Override
    public void findPassword(String phone, String password, String verifyCode) {
        forgotPsdRepository.findPassword(phone,password,verifyCode)
                .observeForever(new Observer<Resource<String>>() {
                    @Override
                    public void onChanged(Resource<String> stringResource) {
                        switch (stringResource.getStatus()) {
                            case ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(stringResource.getMessage(), false);
                                }

                                break;
                            case RESULT_ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(stringResource.getMessage(), false);
                                }

                                break;
                            case LOADING:
                                if (v != null) {
                                    v.showLoadingDialog();
                                }
                                break;
                            case SUCCESS:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    ToastUtils.showToast("密码重置成功！");
                                    v.onFindPassWordSucess();
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    public void getVerifyCode(String phone) {
        forgotPsdRepository.getVerifyCode(phone)
                .observeForever(new Observer<Resource<VerifyCodeResponse>>() {
                    @Override
                    public void onChanged(Resource<VerifyCodeResponse> verifyCodeResponseResource) {
                        switch (verifyCodeResponseResource.getStatus()) {
                            case ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(verifyCodeResponseResource.getMessage(), false);
                                }

                                break;
                            case RESULT_ERROR:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    v.showErrorDialog(verifyCodeResponseResource.getMessage(), false);
                                }

                                break;
                            case LOADING:
                                if (v != null) {
                                    v.showLoadingDialog();
                                }
                                break;
                            case SUCCESS:
                                if (v != null) {
                                    v.dismissLoadingDialog();
                                    ToastUtils.showToast("验证码请求成功，请注意查收短信");
                                    v.onGetVerifyCodeSucess();
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    public ForgotPsdContact.Presenter attachView(ForgotPsdContact.View view) {
        this.v = view;
        return this;
    }

    @Override
    public void detachView() {
        v = null;
    }
}
